package cn.exrick.xboot.base.serviceimpl

import cn.exrick.xboot.base.entity.SafetyAcceptor
import cn.exrick.xboot.base.entity.SecurityAdvisor
import cn.exrick.xboot.base.mapper.SafetyAcceptorMapper
import cn.exrick.xboot.base.service.IEnterpriseUserService
import cn.exrick.xboot.base.service.ISafetyAcceptorService
import cn.exrick.xboot.base.service.ISecurityAdvisorService
import cn.exrick.xboot.core.common.utils.PageUtil
import cn.exrick.xboot.core.common.utils.ResultUtil
import cn.exrick.xboot.core.common.utils.SecurityUtil
import cn.exrick.xboot.core.common.utils.ToolUtil
import cn.exrick.xboot.core.common.vo.PageVo
import cn.exrick.xboot.core.common.vo.Result
import cn.exrick.xboot.core.service.RoleService
import cn.exrick.xboot.core.service.mybatis.IUserRoleService
import com.baomidou.mybatisplus.extension.kotlin.KtQueryWrapper
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl
import com.google.common.collect.Maps
import lombok.extern.slf4j.Slf4j
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional

/**
 * 安全验收人接口实现
 * @author Fei
 */
@Slf4j
@Service
@Transactional
open class ISafetyAcceptorServiceImpl : ServiceImpl<SafetyAcceptorMapper?, SafetyAcceptor?>(), ISafetyAcceptorService {


    @Autowired
    private lateinit var securityUtil: SecurityUtil

    @Autowired
    private lateinit var iEnterpriseUserService: IEnterpriseUserService

    @Autowired
    private lateinit var iSecurityAdvisorService: ISecurityAdvisorService

    override fun createSafetyAcceptor(securityAdvisorIds: Array<out String>): Result<Any?>? {

        return try {

            securityAdvisorIds.forEach {


               if (ToolUtil.isNotEmpty( getOne(KtQueryWrapper(SafetyAcceptor::class.java).eq(SafetyAcceptor::enterpriseId, iEnterpriseUserService.getEnterpriseIdByEmployeeUserId(securityUtil.currUser.id))
                               .eq(SafetyAcceptor::securityAdvisorId, it)))) {

                   return ResultUtil.error(201,"已经添加过该安全顾问")
               }

                save(SafetyAcceptor().apply {

                    enterpriseId = iEnterpriseUserService.getEnterpriseIdByEmployeeUserId(securityUtil.currUser.id)
                    securityAdvisorId = it
                })
            }
            ResultUtil.success("添加成功")

        } catch (e : Exception) {

            ResultUtil.error(e.message)
        }
    }

    override fun removeSafetyAcceptor(ids: Array<out String>?): Result<Any?>? {

        return try {

            removeByIds(ids?.toMutableList())
            ResultUtil.success("移除成功")
        } catch (e : Exception) {

            ResultUtil.error(e.message)
        }
    }

    override fun enterpriseGetSafetySecurityAdvisorList(pageVo: PageVo): Result<MutableMap<String, Any?>>? {

       return ToolUtil.setListToNul(list(KtQueryWrapper(SafetyAcceptor::class.java)
                .eq(SafetyAcceptor::enterpriseId, iEnterpriseUserService.getEnterpriseIdByEmployeeUserId(securityUtil.currUser.id))
                .orderByDesc(SafetyAcceptor::createTime)))?.run {

            val result: MutableMap<String, Any?> = Maps.newHashMap()
            result["size"] = size
            result["content"] = PageUtil.listToPage(pageVo, mapNotNull { iSecurityAdvisorService.getSecurityAdvisorVo(it?.securityAdvisorId!!) })

            ResultUtil.data(result, "获取企业安全验收人成功")
        } ?: ResultUtil.error(201, "暂无数据")
    }

    override fun safetySecurityAdvisorGetEnterprise(pageVo: PageVo): Result<MutableMap<String, Any?>>? {

       return ToolUtil.setListToNul(list(KtQueryWrapper(SafetyAcceptor::class.java)
                .eq(SafetyAcceptor::securityAdvisorId, iSecurityAdvisorService.getOne(KtQueryWrapper(SecurityAdvisor::class.java)
                        .eq(SecurityAdvisor::userId, securityUtil.currUser.id))?.id)
                .orderByDesc(SafetyAcceptor::createTime)))?.run {

            val result: MutableMap<String, Any?> = Maps.newHashMap()
            result["size"] = size
            result["content"] = PageUtil.listToPage(pageVo, mapNotNull { iEnterpriseUserService.getEnterpriseUserVo(it?.enterpriseId!!) })

            ResultUtil.data(result, "获取企业数据成功！")
        } ?: ResultUtil.error(201, "暂无数据")
    }

}